
package com.example.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 限流示例
 *
 */
@RestController
@RequestMapping("/api")
public class FlowControlController {
    
    // 基于QPS的流量控制示例 - 快速失败模式
    @GetMapping("/qps-limit")
    @SentinelResource(value = "qpsLimit", blockHandler = "handleQpsBlock")
    public Map<String, Object> qpsLimit() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "QPS限流测试成功");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    // QPS限流异常处理方法
    public Map<String, Object> handleQpsBlock(BlockException ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 429);
        result.put("message", "请求过于频繁，请稍后重试");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    // 基于线程数的流量控制示例
    @GetMapping("/thread-limit")
    @SentinelResource(value = "threadLimit", blockHandler = "handleThreadBlock")
    public Map<String, Object> threadLimit() throws InterruptedException {
        // 模拟耗时操作
        Thread.sleep(1000);
        
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "线程数限流测试成功");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    // 线程数限流异常处理方法
    public Map<String, Object> handleThreadBlock(BlockException ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 429);
        result.put("message", "系统繁忙，请稍后重试");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
    
    // 关联限流策略示例
    @GetMapping("/read-resource")
    @SentinelResource(value = "readResource", blockHandler = "handleBlock")
    public Map<String, Object> readResource() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "读取资源成功");
        result.put("data", "这是读取的数据");
        return result;
    }
    
    @GetMapping("/write-resource")
    @SentinelResource(value = "writeResource", blockHandler = "handleBlock")
    public Map<String, Object> writeResource() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 200);
        result.put("message", "写入资源成功");
        result.put("data", "写入操作已完成");
        return result;
    }
    
    // 通用限流异常处理方法
    public Map<String, Object> handleBlock(BlockException ex) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 429);
        result.put("message", "资源访问受限");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }
}
